/**
 * @Description: 谷歌在线地图
 * @Author: xubaoliang
 * @Date: 2022-01-25 09:19:27
 */
import TileLayer from "ol/layer/Tile";
import {XYZ} from "ol/source";
import {Map, View} from "ol";

let googleMap = {
  /**
   * 初始化百度地图
   * @param elId
   * @param configs
   * @param options
   * @return {Map}
   */
  init: function (elId, configs, options) {
    let tileUrl = "";
    if(configs.mapType.toLowerCase() === 'googlemap') {
      tileUrl = process.env.VUE_APP_MAP_TILE + "/{z}/{x}/{y}.png";
    }
    else {
      tileUrl = "http://www.google.com/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i345013117!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0";
    }
    let mapLayer = new TileLayer({
      id: 'baseLayer',
      source: new XYZ({
        url: tileUrl,
        crossOrigin: 'anonymous'
      })
    });

    let mapApp = new Map({
      target: elId,
      layers: [mapLayer],
      view: new View({
        projection: "EPSG:4326",    //使用这个坐标系
        center: options.center || configs.mapCenter,
        zoom: options.zoom || configs.defaultZoom,
        minZoom: options.minZoom || configs.minZoom,
        maxZoom: options.maxZoom || configs.maxZoom,
        // 设置缩放级别为整数
        constrainResolution: true,
        // 关闭无级别缩放地图
        smoothResolutionConstraint: false
      }),
      controls: []
    });
    return mapApp;
  }
}

export default googleMap;
